<?php
/**
 *
 *
 * @package
 * @subpackage
 * @author J.Ducastel <nospam0@ducastel.name>
 */

/**
 * A generic database object storage
 */
class DjStore_Db extends WakTool {

    /**
     * required to be used as a webappkit tool
     */
    public static function toolFactory($kit_id, $cfg=array()) {
        return new DjStore_Db($kit_id, $cfg);
    }

    /**
     *
     * @param
     * /
    public function __construct() {

    }/**/

    /* public methods, then properties */

    /**
     * Filters current selection on a key/value pair
     *
     * @param  string $key
     * @param  mixed $value
     * @return DjStore a copy of itself refined with new filter
     */
    public function filter($key, $value) {

    }

    /**
     * Applies a list of filters, grouped in associative array, to the selection
     *
     * @param array $filters
     * @return bool True if all filters have been applied
     * /
    public function filters($filters);/**/

    /**
     * Count items belonging to the current selection
     *
     * @return int
     */
    public function count() {

    }

    /**
     * Return items belonging to the current selection
     *
     * @return array
     */
    public function all() {

    }

    /**
     * Get an item, either the first in selection or by primary key
     *
     * @param  mixed $key primary key value.
     * if null, will return the first item in selection
     * @return object
     */
    public function get($key=null) {
        $sql = $this->_sqlGet($key); // echo $sql;
        $link = DbLinkTool::getActive();
        return $this->factory($link->getResult($sql));
    }

    /**
     * Insert an object into the collection
     *
     * Updates object if necessary (set auto-incremented key, etc)
     *
     * @param  object $item
     * @return bool
     */
    public function insert(&$item) {
        $sql = $this->_sqlInsert($key); // echo $sql;
        return $this->_link()->executeSql($sql);
    }

    /**
     * Update an object within the collection
     *
     * Updates object if necessary (set auto-incremented key, etc)
     *
     * @param  object $item
     * @return bool
     */
    public function update(&$item) {

    }

    /**
     * Remove an object from the collection
     *
     * @param  object $item
     * @return bool
     */
    public function remove($item) {

    }

    /* private methods, then properties */

    /**
     * get a working DbLink
     * @return DbLinkTool
     */
    protected function _link() {
        return DbLinkTool::getActive();
    }

    protected function db_table() {
        return strtolower($this->model()) . 's';
    }

    protected function model() {
        return $this->cfg['model'];
    }

    /**
     * builds a model instance from database result
     * @param DbResult $result
     * @return DjModel
     */
    protected function factory(&$result) {
        if ($obj =$result->fetchObject($this->model()))
            return $obj;
        // echo var_dump($result);
    }

    /**
     * builds load sql
     * @param mixed $key primary key value
     * @return string SQL query
     */
    protected function _sqlGet($key) {
        return 'SELECT * FROM '.$this->db_table()
        .' WHERE id = '.$key;
    }

    /**
     * builds insert sql
     * @param mixed $key primary key value
     * @return string SQL query
     */
    protected function _sqlInsert($key) {
        return '';
        return 'SELECT * FROM '.$this->db_table()
        .' WHERE id = '.$key;
    }

    /**
     * @var string model class
     */
    protected $_model;
}